
//$debug.setMemoryLeakDetectionEnabled(false);//关闭内存泄露检测
if (!images.requestScreenCapture()) {
    console.log("申请截图权限不成功!!");
    exit();
} else {
    console.log("申请截图权限成功~~");
}
var intX = 0,intY = 0;
var 延时 = 3000;
var 图片路径 =String(engines.myEngine().getSource()).replace("拉珍珠.js","");
device.wakeUp();
// var 图片路径 = "/storage/emulated/0/DCIM/Screenshots/";

//定时器
var id1 = setInterval(function(){	//循环定时器id
    拉珠();
}, 600*1000);

//30s后取消循环
setTimeout(function(){			//单次定时器
    clearInterval(id1);				//清除循环定时器id1
}, 28000*1000);

拉珠();



function 拉珠(){
    msg(图片路径);
    toastLog('开始');
    toastLog("开始找图");

    //检查是否拉领取奖励
    if(找图("领取奖励.png",true)){
        click(700, 340);
        sleep(1000);
        msg("领取奖励.png");
    }
    
    //检查是否拉成功
    if(找图("采集成功.png",true)){
        msg("采集成功.png");
    }
    //第一次可能会久一点
    sleep(3000);
    for(let i=0;i<10;i++){
        if(找图("采集成功.png",true)){
            sleep(2000);
            msg("采集成功.png");
        }
    }

    //领取奖励
    if(找图("目标奖励.png",true)){
        //先只做一键领取
        if(找图("一键领取.png",true)){
            点击(960,445,3000);
        }
        点击(960,445,2000);
    }


    //使用助力卡
    
    if(找图("助力卡.png",true)){
            //先只做一键领取
        while(true){
            if(找图("开启1张.png",true)){
                click(700, 340);
                sleep(1000);
                点击(700, 340,2000);
                点击(700, 340,2000);
            }else{
                点击(960,445,2000);
                点击(960,445,2000);
                break;
            }
        }

    }
    

    let 刷新卡量= 找图("零刷新卡.png",true);
    

//刷新
    if(找图("免费刷新.png",true)){
            msg("免费刷新.png");
    }else if(找图("刷新卡.png",true)){
            msg("刷新卡.png");
            if(找图("确定.png",true)){
                msg("确定.png");
            }
        }
        // else{
        //     return 刷新卡量;
        // }
    
    var 拉珠数量1 = 一图多找("一级.png");
    var 拉珠数量2 = 一图多找("二级.png");
    var 拉珠数量3 = 一图多找("三级.png");
    toastLog(拉珠数量1);
    toastLog(拉珠数量2);
    toastLog(拉珠数量3);
    每级拉珠(拉珠数量1,1);
    每级拉珠(拉珠数量2,1);
    每级拉珠(拉珠数量3,1);
    
    msg("采集结束");
    return 刷新卡量;
    
}

function 每级拉珠(拉珠数量,上兽数量){
    拉珠数量.forEach(point => {
        

        //防止弹广告
        click(700, 340);
        sleep(1000);

        //防止弹采集成功
        if(找图("采集成功.png",true)){
            msg("采集成功.png");
        }

        click(point.x-20, point.y-20)
        sleep(3*1000);
        toastLog("开始找采集");
        
        if(找图偏移点击("采集.png",true,30,20)){
            msg("采集");
            上兽拉珠(上兽数量);
        }

        //点错采集 有调整内容
        if(找图("调整阵容.png",false)){
            click(700, 340);
            msg("调整阵容.png");
        }

    });
  }


function 点击(intX,intY,sleepTime){
  click(intX,intY);
  sleep(sleepTime);
}

function msg(msgValue){
    toastLog(msgValue);
    console.log(msgValue);
}


function 找图(imgPath,isClick){
    let res = false;
    let myPath = 图片路径;
    //let myPath = "/storage/emulated/0/DCIM/Screenshots/";
    let wx = images.read(myPath + "res/" + imgPath);
    try{
        // 截图并找图
        let img = images.captureScreen();//截屏
        let p = findImage(img, wx);
        if(p){
            intX = p.x;
            intY = p.y;
            if(isClick){
                点击(intX,intY,延时);
            }
            res = true;
        }
        wx.recycle();//释放内存
        img.recycle();//释放内存
    }
    catch(err){
        //console.error(err);
    }
    
    return res;
}

function 找图偏移点击(imgPath,isClick,yidongX,yidongY){
    let res = false;
    let myPath = 图片路径;
    //let myPath = "/storage/emulated/0/DCIM/Screenshots/";
    let wx = images.read(myPath + "res/" + imgPath);
    try{
        // 截图并找图
        let img = images.captureScreen();//截屏
        let p = findImage(img, wx);
        if(p){
            intX = p.x+yidongX;
            intY = p.y+yidongY;
            if(isClick){
                点击(intX,intY,延时);
            }
            res = true;
        }
        wx.recycle();//释放内存
        img.recycle();//释放内存
    }
    catch(err){
        //console.error(err);
    }
    return res;
}


function 一图多找(imgPath){
        let myPath = 图片路径;
        let points;
        //let myPath = "/storage/emulated/0/DCIM/Screenshots/";
        let wx = images.read(myPath + "res/" + imgPath);
        try{
            // 截图并找图
            var img = captureScreen();
              points = images.matchTemplate(img, wx, {
                threshold: 0.9,
                max:10
            }).points;
    
            img.recycle();//释放内存
        }
        catch(err){
            //console.error(err);
        }

        wx.recycle();//释放内存
        img.recycle();//释放内存
        
        return points;
    }


function 上兽拉珠(上兽数量){
    sleep(3*1000);
    if(上兽数量=1){
        sleep(1*1000);
        click(300,1600);
        sleep(1*1000);
    }else{
        sleep(1*1000);
        click(300,1600);
        sleep(1*1000);
    }
    
click(840,2100);
sleep(1*1000);
}